package com.duolebo.uteped_sdk.msg;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.UTEPedEventHandler;
import com.duolebo.uteped_sdk.utils.Const;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yc.nadalsdk.bean.DeviceOperatorConfig;
import com.yc.nadalsdk.bean.MenstrualCapability;
import com.yc.nadalsdk.bean.MenstrualCycleData;
import com.yc.nadalsdk.bean.MenstrualNotify;
import com.yc.nadalsdk.bean.Response;
import com.yc.nadalsdk.ble.open.UteBleConnection;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MenstrualMessage extends BaseMessage {

    private static final String PATH = "menstrual";

    /**
     * BaseCallback
     *
     * @param methodChannel methodChannel
     * @param eventHandler  eventHandler
     */
    public MenstrualMessage(MethodChannel methodChannel, UTEPedEventHandler eventHandler) {
        super(methodChannel, eventHandler);
    }

    @Override
    public String getPath() {
        return PATH;
    }

    @Override
    public boolean methodCallConsumed(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        if (!call.hasArgument(Const.KEY_METHOD))
            return false;

        if (super.methodCallConsumed(call, result))
            return true;

        UteBleConnection uteBleConnection = uteBleClient.getUteBleConnection();
        boolean consumed = true;
        String method = call.argument(Const.KEY_METHOD);
        assert method != null;

        Response<?> resp;
        switch (method) {
            case Const.M_setMenstrualNotifyAbility:
                if (call.hasArgument(Const.TYPE_MAP)) {
                    Map<String, Object> map = call.argument(Const.TYPE_MAP);
                    assert map != null;
                    d(map.toString());

                    List<MenstrualNotify> menstrualNotifies = new ArrayList<>();
                    boolean enable = (boolean) map.get("enable");
                    if (enable) {
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.ACTIVATION_SWITCH, true));
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.NOTIFY_ON_EASY_TO_PREGNANT_START, true));
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.NOTIFY_ON_EASY_TO_PREGNANT_END, true));
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.NOTIFY_ON_MENSTRUAL_START, true));
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.NOTIFY_ON_MENSTRUAL_END, true));
                    } else {
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.ACTIVATION_SWITCH, false));
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.NOTIFY_ON_EASY_TO_PREGNANT_START, false));
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.NOTIFY_ON_EASY_TO_PREGNANT_END, false));
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.NOTIFY_ON_MENSTRUAL_START, false));
                        menstrualNotifies.add(new MenstrualNotify(MenstrualNotify.NOTIFY_ON_MENSTRUAL_END, false));
                    }

                    d("setMenstrualNotifyAbility list =" + new Gson().toJson(menstrualNotifies));
                    resp = uteBleConnection.setMenstrualNotifyAbility(menstrualNotifies);
                    d("setMenstrualNotifyAbility response =" + new Gson().toJson(resp));
                    result.success(resp.getErrorCode() == Const.CODE_SUCCESS);
                } else {
                    result.success(false);
                }
                break;

            case Const.M_setMenstrualCycleData:

                if (call.hasArgument(Const.TYPE_MAP)) {
                    Map<String, Object> map = call.argument(Const.TYPE_MAP);
                    assert map != null;
                    d(map.toString());

                    String stringMap = new Gson().toJson(call.argument(Const.TYPE_MAP), new TypeToken<Map<String, Object>>(){}.getType());
                    MenstrualCycleData data = new Gson().fromJson(stringMap, MenstrualCycleData.class);
//
//                    MenstrualCycleData mcd = new MenstrualCycleData();
//                    mcd.setHistoryCycleSize(1);
//                    mcd.setFutureCycleSize(1);
//                    mcd.setModifyTime(getDateInSeconds());
//
//                    // 用户手动输入的生理周期长度
//                    if (map.containsKey("manualCycleDays"))
//                        mcd.setManualCycleDays((Integer) map.get("manualCycleDays"));
//                    // 用户手动输入的经期长度
//                    if (map.containsKey("manualMenstrualDays"))
//                        mcd.setManualMenstrualDays((Integer) map.get("manualMenstrualDays"));
//
//                    // 女性生理周期数据列表
//                    List<MenstrualCycleData.MenstrualData> menstrualDataList = new ArrayList<>();
//                    if (map.containsKey(Const.TYPE_LIST)) {
//                        List<Map<String, Object>> modelMap = (List<Map<String, Object>>) map.get(Const.TYPE_LIST);
//
//                        for (Map<String, Object> objectMap : modelMap) {
//                            MenstrualCycleData.MenstrualData data = new MenstrualCycleData.MenstrualData();
//                            if (objectMap.containsKey("menstrualStartTime"))
//                                data.setMenstrualStartTime((Integer) objectMap.get("menstrualStartTime"));
//                            if (objectMap.containsKey("menstrualEndTime"))
//                                data.setMenstrualEndTime((Integer) objectMap.get("menstrualEndTime"));
//                            if (objectMap.containsKey("easyToPregnantStartTime"))
//                                data.setEasyToPregnantStartTime((Integer) objectMap.get("easyToPregnantStartTime"));
//                            if (objectMap.containsKey("easyToPregnantStartTime"))
//                                data.setEasyToPregnantEndTime((Integer) objectMap.get("easyToPregnantStartTime"));
//                            if (objectMap.containsKey("cycleDays"))
//                                data.setCycleDays((Integer) objectMap.get("cycleDays"));
//
//                            data.setIsManual(true);
//                            menstrualDataList.add(data);
//                        }
//                    }

//                    mcd.setMenstrualDataList(menstrualDataList);

                    resp = uteBleConnection.setMenstrualCycleData(data);

                    d("setMenstrualCycleData response =" + new Gson().toJson(resp));
                    result.success(resp.getErrorCode() == Const.CODE_SUCCESS);
                }
                break;

            case Const.M_setAppMenstrualCapability:
                int capability = 2;
                if (call.hasArgument(Const.KEY_VALUE)) {
                    capability = call.argument(Const.KEY_VALUE);
                }
                MenstrualCapability mc = new MenstrualCapability();
                mc.setCapability(capability);

                resp = uteBleConnection.setAppMenstrualCapability(mc);
                result.success(resp.getErrorCode() == Const.CODE_SUCCESS);
                break;

            case Const.M_setAppMenstrualModifyTime:
                int mt = (int) (new Date().getTime() / 1000);
                if (call.hasArgument(Const.KEY_VALUE)) {
                    mt = call.argument(Const.KEY_VALUE);
                }
                resp = uteBleConnection.setAppMenstrualModifyTime(mt);
                result.success(resp.getErrorCode() == Const.CODE_SUCCESS);
                break;
            default:
                consumed = false;
        }

        return consumed;
    }

    int getDateInSeconds() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0);
        return (int) (cal.getTimeInMillis() / 1000);
    }
}
